<?php
class UserIdentity extends CUserIdentity
{
    private $_id;

     /**
    * @param $name 登录名 ，
    * @param  $password 密码
    * @return boolean whether authentication succeeds.
    */
    public function __construct($username,$password)
    {
        $this->username=$username;
        $this->password=$password;

    }
    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {   

        $criteria = new CDbCriteria;

            $criteria->addColumnCondition(array('is_deleted'=>0, 
                'mobile'=>$this->username));
            $user=User::model()->find($criteria);
            
        if($user===null){
             // Yii::app()->user->setFlash('error', "<p style=color:red>该用户不存在</p>");
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        }else if(!$user->validatePassword($this->password)) {
            // Yii::app()->user->setFlash('error', "<p style=color:red>密码错误</p>");
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        }else if($user->validatePassword($this->password)&&($user->status == User::STATUS_DISABLE)) {
            // Yii::app()->user->setFlash('error', "<p style=color:red>你的账户已被禁用，请联系管理员</p>");
            $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
        }
        else 
        {
            $this->_id=$user->sid;

             Yii::app()->cache->set($this->_id,$this->username);
             Yii::app()->cache->set('last_login_time'.$this->id,$user->last_login_time);

            $this->errorCode=self::ERROR_NONE;
            
            $user->last_login_time = time();
            $user->save();
        }
        return $this->errorCode==self::ERROR_NONE;
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId()
    {
        return $this->_id;
    }


}
?>
